import {postBanner, postWishes} from '@/base/api/BannersApi';
import {deepAssign} from '@/base/util/Objects';
import {getAgentList} from '@/base/api/AgentApi';
import {Page} from '@/base/Page';
import {getCompanyList} from '@/base/api/CompanyApi';
import {Company} from '@/base/model/Company';
import {Agent} from '@/base/model/Agent';

export class FormBloc {
  model: any = {
    companyId: 0,
    agentId: 1,
    wishName: '',
    imageurl: '',
    isAble: 0,
    targetGroup: 'ALL_MEMBER',
    targetAccounts: [],
    type: 'IMAGE',
    screenOpeningTime:10
};
  loading = false;
  fileList: Array<any> = []
  num: number = 0;
  sortList: any = [];
  companyList: any = []
  // sortId: number = 0;
  // modelId: number = 0;
  isDetail = false;
  isinfo = false;
  type: string | undefined;
  company: Page<Company> = new Page<Company>([], 0, 1, 10, 0, []);
  angent: Page<Agent> = new Page<Agent>([], 0, 1, 15, 0, []);

  getAllSort(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    let that = this;
    return getAgentList({
      pageIndex,
      pageSize,
      companyId: this.model.companyId
    }).then((res) => {
      this.sortList = res.data.items.map(function (item) {
        var obj = {id: 0, agentName: ''};
        // @ts-ignore
        obj.id = item.id;
        obj.agentName = item.agentName;
        return obj;
      });
      return this.sortList;
    }).finally(() => {
      this.loading = false;
    });
  }

  getCompanyList(pageIndex: number,
                 pageSize: number,
                 sortDirection: string) {
    return getCompanyList({pageIndex, pageSize, sortDirection, name: '', createBegin: ''})
      .then((res) => {
        deepAssign(this.company, res.data)
        this.companyList = res.data.items.map(item => {
          let obj = {companyId: 0, companyName: ''}
          obj.companyId = item.id;
          obj.companyName = item.name
          return obj
        })
        return res;
      }).finally(() => {
        this.loading = false;
      });
  }

  addModel(model) {
    this.loading = true;
    return postWishes({
      agentId: model.agentId,
      wishName: model.wishName,
      // targetGroup: model.targetGroup,
      companyId: model.companyId,
      wishUrl: model.imageurl,
      isAble: model.isAble,
      memberIds: model.targetAccounts,
      type: model.type,
      startTime:model.startTime,
      endTime:model.endTime,
      screenOpeningTime:model.screenOpeningTime,
      linkUrl:model.linkUrl,
      isAll:model.isAll,
    }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  reset() {
    location.reload();
  }

}
